home *** CD-ROM | disk | FTP | other *** search
- Path: ramses.eurocontrol.fr!usenet
- From: Jean-Christophe COTA - COT <Jean-Christophe.Cota@eurocontrol.fr>
- Newsgroups: comp.lang.c++
- Subject: VIRTUAL INHERITANCE: base class TO derived class object CONVERSION
- Date: Mon, 25 Mar 1996 12:20:11 +0100
- Organization: EUROCONTROL - Centre Experimental
- Message-ID: <3156816B.4E8A@eurocontrol.fr>
- NNTP-Posting-Host: berlioz.eurocontrol.fr
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/755)
-
- I'm dealing with a class hierarchy which uses multiple inheritance as
- well as polymorphism. It was decided, for some reason, to use systema-
- -tically virtual inheritance.
-
- The problem is that I need to be able to convert explicitly a base
- class object to a derived class one, which is impossible in the virtual
- inheritance paradigm contrary to the static one.
-
- I guess that this is a very classical question (sorry, I've not found
- any clue in the "FAQ posting"), and I'm sure that the need for such
- feature will raise different opinions in the C++ experts community.
- The fact is I don't really catch the philosophical reason (sure, there
- must be implementation one) why C++ allows the operation in one case
- (even if it's dangerous) and forbids it in the other...
-
- However, I've tried the following test (please, don't scream ;-)), but
- it leads to undefined and dangerous operations. I don't have enough
- knowledge on virtual tables management to really understand what's
- happening, perhaps someone will be kind enough and try to explain it
- to me or give me some references where I could get the info.
-
- class base {
- public:
- virtual void* _deref() = 0;
- };
-
- class derived_1 : virtual public base {
- public:
- virtual void foo_1() {};
- };
-
- class derived_2 : virtual public base {
- public:
- virtual void foo_2() {};
- };
-
- class derived_3 : virtual public derived_2 {
- public:
- void foo_2() { cout << "derived_3::foo_2()" << endl; };
- };
-
- class final_1 : virtual public derived_1, virtual public derived_3 {
- public:
- void* _deref() { cout << "final_1::_deref()" << endl; return this; };
- void foo_1() { cout << "final_1::foo_1()" << endl; };
- };
-
- class final_2 : virtual public derived_1, virtual public derived_3 {
- public:
- void* _deref() { cout << "final_2::_deref()" << endl; return this; };
- void foo_1() { cout << "final_2::foo_1()" << endl; };
- };
- ...
-
-
- main() {
- derived_1 *d1;
- derived_2 *d2;
-
- d1 = new final_1;
-
- d1->foo_1();
- d2 = (derived_2 *)(d1->_deref());
- d2->foo_2();
- }
-
-
- This program produces the following:
-
- final_1::foo_1()
- final_1::_deref()
- final_1::_deref()
-
- The purpose of this program is:
- I've got a derived_1 class object which I know is a final_X object,
- and I want to call on this object the virtual functions foo_1() and
- foo_2().
-
- I guess I'm quite lucky it does not merely explode and dump a core
- while executing the last instruction... because, as you can see,
- what I expected to be the foo_2 entry in the *d2 object virtual
- table ends to be the final_1::_deref() function: in the third line
- I expected to see "derived_3::foo_2()" and not "final_1::_deref()".
-
- Of course, if derived_3 statically inherits from derived_2 and
- final_X statically inherit from derived_3 and derived_1, the whole
- thing will work but, as I said at the begining, I'm not supposed
- to use nonvirtual inheritance...
-
- Any help will be appreciated.
-
- --
- --------------------------------------------------------------------
- Mr Jean-Christophe Cota | Systems Architecture (ARH)
- Jean-Christophe.Cota@eurocontrol.fr |
- http://www.eurocontrol.fr | EUROCONTROL Centre Experimental
- tel: +33 1 69887683 | BP15, 91222 Bretigny Cedex
- fax: +33 1 69887227 | FRANCE
- ---------------------------------------------------------------------
-